<?php


namespace app\manage\model;


use think\Db;

class Store
{

    /**
     * 店铺列表  type 1=上架  2=下架
     */
    public function storeList($filter,$page,$page_size,$type){
        $where=[];
        $where['sa.status']=1;

        //行业分类
        if (!empty($filter['ic_id'])) {
            $where['sa.ic_id'] = $filter['ic_id'];
        }
        //城市
        $city = empty($filter['city']) ? '' : $filter['city'];
        if(!empty($city)){
            $where['sa.address'] = ['like', "%".$city."%"];
        }

       /* $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['sa.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['sa.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['sa.create_time'] = ['<= time', $endTime];
            }
        }*/
        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['sa.create_time'] = [['>= time', $start], ['<= time', $end]];
        }


        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['sa.company_name|sa.contact|sa.phone'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['sa.is_dismount']=1;
        }else{
            $where['sa.is_dismount']=2;
        }

        $field='sa.sa_id,sa.company_name,sa.contact,sa.address,sa.phone,sa.ic_id,sa.create_time';
        $data=Db::name('store_apply')->alias('sa')
            ->field($field)
            //->join('industry_class ic','ic.ic_id=sa.ic_id')
            ->where($where)
            ->page($page,$page_size)
            ->select();

        foreach ($data as $k=>&$v){
            $industry=Db::name('industry_class')
                ->where('ic_id',$v['ic_id'])
                ->find();
            if(empty($industry)){
                $v['ic_name']='';
            }else{
                $v['ic_name']=$industry['ic_name'];
            }
            unset($v['ic_id']);
        }

        $total_num=Db::name('store_apply')->alias('sa')
            //->join('industry_class ic','ic.ic_id=sa.ic_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 下架店铺   type 1=下架单个  2=批量下架
     */
    public function dismount($id,$type){
        if($type==1){
            Db::name('store_apply')
                ->where(['sa_id'=>$id])
                ->update(['is_dismount'=>2,'update_time'=>time()]);
        }else{
            Db::name('store_apply')
                ->where(['sa_id'=>['in',$id]])
                ->update(['is_dismount'=>2,'update_time'=>time()]);
        }

        return true;
    }

    /**
     * 详情
     */
    public function storeDetail($id){
        $field='credit_code,mch_id,number,investment_phone,introduce,card_just,card_back,business_license';
        $data=Db::name('store_apply')
            ->field($field)
            ->where('sa_id',$id)
            ->find();
        return $data;
    }

    /**
     * 店铺行业分类  industry_class
     */
    public function industry(){
        $data=Db::name('industry_class')
            ->field('ic_id,ic_name')
            ->select();
        return $data;
    }

    /**
     * 店铺所在城市列表
     */
    public function selectStoreAddress(){
       /* $data=Db::name('store_apply')
            ->distinct('address')
            ->field('address')
            ->where(['address'=>['neq',''],'status'=>1,'is_delete'=>1])
            ->select();*/
        $data=Db::name('store_apply')
            ->field('longitude,latitude')
            ->where(['status'=>1,'is_delete'=>1])
            ->select();

        $list=[];
        if(!empty($data)){
            foreach ($data as $k=>&$v){
                $res=$this->get_my_addr_infos($v['longitude'],$v['latitude']);
                if($res!='定位不准确，地图上没该地方'){
                    if(!empty($res['province']) && !empty($res['city'])){
                        $v['address']=$res['province'].$res['city'];
                        $list[$k]=$res['province'].$res['city'];
                    }
                }
            }
        }

        $test2=array_unique($list);
        $test3=array_values($test2);

        $info['address']= $test3;

        return $info;

    }

    function get_my_addr_infos($lng,$lat){
        $ch = curl_init();
        $timeout = 5;
        $lat = $lat; //维度
        $lng = $lng; //经度
        $URL = "http://api.map.baidu.com/geocoder/v2/?ak=nkovf6GWmnxwwbvN4Gez4tfl&location=$lat,$lng&output=json&pois=0";
        curl_setopt ($ch, CURLOPT_URL, $URL);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        $positon_add = json_decode($file_contents,true);
        //dump($positon_add);
        //$positon_addr_detail = ($positon_add['result']['formatted_address']) ? $positon_add['result']['formatted_address'] : "定位不准确，地图上没该地方";
        //return $positon_addr_detail;
        $data=($positon_add['result']['addressComponent']) ? $positon_add['result']['addressComponent'] : "定位不准确，地图上没该地方";
        return $data;
    }


}